<template>
    <el-card>
        <el-card>
            <el-row class="form-row">
                <el-form :model="filters" label-position="right" inline ref="formInline">
                    <el-form-item label="小区">
                        <div class="filters-input">
                            <el-select v-model="filters.name" placeholder="请选择小区" filterable clearable
                                       no-data-text="暂无数据">
                                <el-option v-for="item in houseList" :value="item.name" :key="item.name"
                                           :label="item.name"></el-option>
                            </el-select>
                        </div>
                    </el-form-item>
                    <el-form-item>
                        <Button type="primary" icon="ios-search" @click="handleQuery('formInline')">搜索</Button>
                    </el-form-item>
                </el-form>
            </el-row>
            <el-row class="form-row">
                <el-button type="primary" @click="addSource()">新增</el-button>
            </el-row>
            <el-table stripe border :loading="tableLoading" :data="tableData" ref="table" style="width: 100%">
                <template slot="empty">暂无数据</template>
                <el-table-column label="数据来源" prop="name"></el-table-column>
                <el-table-column label="小区名称" prop="housingName"></el-table-column>
                <el-table-column label="操作" width="150" align="center">
                    <template slot-scope="scope">
                        <el-button size="mini" type="primary" @click="addSource(scope.row)">修改
                        </el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <Page style="margin: 10px 0;" :total="pageTotal" :current="pageNum" :page-size="pageSize" show-total
                  id="lyPage"
                  show-sizer show-elevator @on-change="handlePage" @on-page-size-change='handlePageSize'>
            </Page>
        </el-card>

        <el-dialog title="配置小区数据源" :visible.sync="sourceConfig" width="900">
            <div class="pp-info">
                <el-form :model="housingSource" :rules="rules" ref="housingSourceForm" label-width="100px"
                         class="demo-ruleForm">
                    <el-form-item label="数据来源" prop="name">
                        <el-select v-model="housingSource.name" placeholder="请选择数据来源" filterable
                                   allow-create clearable style="width:100%">
                            <el-option v-for="item in sourceList" :value="item.name" :key="item.name"
                                       :label="item.name"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="小区" prop="housingId">
                        <treeselect noChildrenText="没有子节点" noOptionsText="暂无选项"
                                    noResultsText="没有发现结果" placeholder="选择小区" v-model="housingSource.housingId"
                                    :multiple="true" :disable-branch-nodes="true"
                                    :options="treeList"/>
                    </el-form-item>
                </el-form>
            </div>
            <div slot="footer" class="dialog-footer">
                <el-button @click="sourceConfig = false">取 消</el-button>
                <el-button type="primary" @click="surehousingSource('housingSourceForm')">确 定</el-button>
            </div>
        </el-dialog>
    </el-card>
</template>


<script>
    //线上下载模板地址
    import baseURL from "_conf/url";
    import Treeselect from "@riophae/vue-treeselect";
    import Cookies from "js-cookie";
    import {TOKEN_KEY} from "@/libs/util";
    import moment from "moment"
    //线上上传账单地址
    const act = baseURL + "/api/bill/upload";

    export default {
        components: {
            Treeselect,
        },
        data() {
            return {
                //查询条件
                filters: {},
                //页码
                pageNum: 1,
                //每页数量
                pageSize: 10,
                //总数
                pageTotal: 0,
                tableData: [],
                houseList: [],
                //表单提交状态
                tableLoading: false,
                sourceConfig: false,
                treeList: [],
                housingSource: {
                    housingId:[],
                },
                sourceList: [],
                //校验规则
                rules: {
                    name: [
                        {
                            required: true,
                            message: "请填写数据来源",
                        }
                    ],
                    housingId: [
                        {
                            required: true,
                            message: "请选择小区",
                        }
                    ],
                },
            }
        },
        created: function () {
            this.gethouse();
            this.loadData();

        },
        methods: {
            //加载数据
            loadData: function () {
                if (this.tableLoading) return;
                this.tableLoading = true;
                let params = this.filters;
                params.pageNum = this.pageNum;
                params.pageSize = this.pageSize;

                console.log(params)

                //与后台交换数据
                this.postRequest("/api/housingSource/loadData", params).then(res => {
                    console.log(res);
                    this.tableLoading = false;
                    if (res.code == 20000) {
                        this.tableData = res.data.list;
                        this.pageTotal = res.data.total;
                    } else {
                        this.tableData = [];
                        this.$message.error(res.message);
                    }
                });
            },

            //加载小区
            gethouse() {
                let url = "/api/housing/getHousingByLogin";
                this.postRequest(url).then(res => {
                    this.houseList = res;
                });
            },

            //加载小区树
            treeData() {
                this.getRequest("/api/authInfo/trees").then(res => {
                    if (res.code == 20000) {
                        this.treeList = res.data;
                    } else {
                        this.$message.error(res.message);
                    }
                });
            },
            //加载数据源
            getSourceList() {
                this.getRequest("/api/housingSource/getSourceList").then(res => {
                    if (res.code == 20000) {
                        this.sourceList = res.data;
                    } else {
                        this.$message.error(res.message);
                    }
                });
            },
            //打开新增修改页面
            addSource(row) {
                this.housingSource = {};
                if (row) {
                    this.housingSource = row;
                    this.housingSource.housingId = row.housingIds.split(",");
                }
                this.treeData();
                this.getSourceList();
                this.sourceConfig = true;
            },
            //保存新增修改
            surehousingSource: function (housingSourceForm) {
                this.$refs[housingSourceForm].validate((valid) => {
                    if (valid) {
                        console.log(this.housingSource)
                        this.postRequest("/api/housingSource/saveOrUpdate", this.housingSource).then(res => {
                            if (res.code == 20000) {
                                this.sourceConfig = false;
                                this.$Message.success({
                                    content: "操作成功",
                                    duration: 5
                                });
                                this.loadData();
                            } else {
                                this.$message.error(res.message);
                            }
                        });
                    }
                });
            },

            //删除
            handleDelete(id) {
                console.log(id)
                this.getRequest("/api/housingSource/deleteSource?id=" + id).then(res => {
                    if (res.code == 20000) {
                        this.$Message.success({
                            content: "操作成功",
                            duration: 5
                        });
                        this.loadData();
                    } else {
                        this.$message.error(res.message);
                    }
                });
            },

            //查询
            handleQuery() {
                this.loadData();
            },
            handlePage(value) {
                this.pageNum = value;
                this.loadData();
            },
            handlePageSize(value) {
                this.pageSize = value;
                this.loadData();
            },
        }
    };
</script>
<style lang="less" scoped>
    #pop {
        width: 780px;
        height: 600px;
        display: none;
        position: fixed;
        z-index: 10;
        top: 10%;
        left: 50%;
        margin-left: -390px;
    }
</style>
